package com.atguigu.lease.admin.controller;

import com.atguigu.lease.admin.service.FeeKeyService;
import com.atguigu.lease.admin.service.FeeValueService;
import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.bean.FeeKey;
import com.atguigu.lease.model.bean.FeeValue;
import com.atguigu.lease.model.vo.FeeKeyVo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.atguigu.lease.common.result.Result.ok;

/**
 * @author LOGIN
 * @title FeeKeyController
 * @create 2025/6/24 1:52
 * @description
 */
@Tag(name = "杂费管理模块")
@RestController
@RequestMapping("/admin/fee")
public class FeeKeyController {
    @Autowired
    private FeeKeyService feeKeyService;
    @Autowired
    private FeeValueService feeValueService;

    @Operation(summary = "1、查询所有的杂费名称和值列表")
    @GetMapping("/list")
    public Result<List<FeeKeyVo>> list(){
        return Result.ok(feeKeyService.listFeeKeyVos());
    }
//    /admin/fee/value/saveOrUpdate
    @Operation(summary = "2、新增feeValue")
    @PostMapping("/value/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody FeeValue feeValue){
        return Result.ok(feeValueService.saveOrUpdate(feeValue));
    }
//    /admin/fee/key/saveOrUpdate
    @Operation(summary = "3、新增或更新杂费名称")
    @PostMapping("/key/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody FeeKey feeKey){
        return Result.ok(feeKeyService.saveOrUpdate(feeKey));
    }
//    /admin/fee/value/deleteById
    @Operation(summary = "4、根据id删除杂费值")
    @DeleteMapping("/value/deleteById")
    public Result deleteValueById(@RequestParam("id")Long id){
        return Result.ok(feeValueService.removeById(id));
    }
//    /admin/fee/key/deleteById
    @Operation(summary = "5、根据id删除杂费名称")
    @DeleteMapping("/key/deleteById")
    public Result deleteKeyById(@RequestParam("id")Long feeKeyId ){
        //判断： 如果 FeeKeyId没有可用的 Feevalue 才可以进行删除，反之
        long count = feeValueService.count(Wrappers.lambdaQuery(FeeValue.class)
                .eq(FeeValue::getFeeKeyId, feeKeyId));
        if(count > 0){
            return Result.error(ResultCodeEnum.DELETE_ERROR);
        }
        return Result.ok(feeKeyService.removeById(feeKeyId));
    }

}
